﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ColorWanted.Plugin.RsaTool
{
    public partial class ToolForm : Form
    {
        public ToolForm()
        {
            InitializeComponent();

            cbPaddingType.SelectedIndex = 1;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            var test = "This is an 中文 string.";

            string priv = tbPriv.Text;
            string pub = tbPub.Text;

            string enrypted, decrypted;

            var paddingType = cbPaddingType.SelectedItem.ToString();

            if (string.IsNullOrWhiteSpace(priv))
            {
                MessageBox.Show(this, "请填写私钥", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (string.IsNullOrWhiteSpace(pub))
            {
                MessageBox.Show(this, "请填写公钥", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (priv[0] != '<')
            {
                try
                {
                    Util.LoadPrivateKey(priv, paddingType);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "解析私钥失败:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }

            if (pub[0] != '<')
            {
                try
                {
                    Util.LoadPublicKey(pub);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "解析公钥失败:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }

            try
            {
                enrypted = Util.Encrypt(pub, test, paddingType);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "加密失败:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            try
            {
                decrypted = Util.Decrypt(priv, enrypted);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "解密失败:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (decrypted == test)
            {
                MessageBox.Show(this, "通过", "校验结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            MessageBox.Show(this, "不通过", "校验结果", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void btnGen_Click(object sender, EventArgs e)
        {
            var length = (int)numLength.Value;

            var keypair = Util.MakeKeypair(length);
            tbPriv.Text = keypair.Item1;
            tbPub.Text = keypair.Item2;
        }
    }
}
